package cn.hl.system.controller;

import cn.hl.basic.util.AjaxResult;
import cn.hl.basic.util.PageList;
import cn.hl.system.domain.DictionaryType;
import cn.hl.system.query.DictionaryTypeQuery;
import cn.hl.system.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/dictionaryType")
@Api(value = "数据字典的API",description="数据字典相关的CRUD功能")
public class DictionaryTypeController {

    /** 调service层 */
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public DictionaryType findById(@PathVariable("id") Long id) {
        return dictionaryTypeService.findById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<DictionaryType> findById() {
        return dictionaryTypeService.findAll();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            dictionaryTypeService.deleteDetail(id);
            dictionaryTypeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或修改")
    public AjaxResult addAndUpdate(@RequestBody DictionaryType dictionaryType) {
        try {
            if (dictionaryType.getId() == null) {
                dictionaryTypeService.add(dictionaryType);
            } else {
                dictionaryTypeService.update(dictionaryType);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery dictionaryTypeQuery) {
        return dictionaryTypeService.queryPage(dictionaryTypeQuery);
    }

    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            dictionaryTypeService.deleteDetails(ids);
            dictionaryTypeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！");
        }
    }
}
